// 爬取数据
const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const ShoesModel = require("./models/Shoes");
const branch = require("./static/data") 
async function puppeteerData(data) {
    console.log("start");

    // var mongoData = await AllShoesModel.find({});
    console.log(branch);

    /* 1.新建一个浏览器的实例 */
    const browser = await puppeteer.launch({
        headless: true
    });
    /* 2.新建一个页面 */
    const page = await browser.newPage();
    /* 3.到达某个页面 */
    await page.goto(`https://www.hiwuhuan.com/facetSearch?brandName=${data}`);
    /* 4.在页面停顿3s */
    /* 5.evaluate可以获取页面内容 */
    var content = await page.content();
    var $ = cheerio.load(content, {
        decodeEntities: false
    })
    var arr =[];
    var item = $(".li_2 li");
    item.each((index,value)=>{
        var sold = Math.round(Math.random()*10000);
        var url = $(value).find("a").attr("href");
        var img = $(value).find("a img").attr("src");
        var title = $(value).find(".sp_1").html();
        var detailTitle = $(value).find("a img").attr("alt");
        var price = $(value).find(".sp_3").html().split("￥")[1].trim();
        arr.push({
            url,
            img,
            title,
            detailTitle,
            price,
            sold
        })
    })
    
    /* 6.关闭浏览器 */
    await browser.close();
    
    // 获取详情
    for(let i=0;i<arr.length;i++){
        var detailMsg = await detail(arr[i].url);
        console.log(detailMsg);
        arr[i].detailMsg = detailMsg[0];
        let {url,img,title,detailTitle,price,sold} = arr[i];
        // console.log('detailTitle: ',typeof detailTitle);
        var admin = new ShoesModel({
            url,img,title,detailTitle,price,detailMsg,sold
        })
        admin.save(function(err,ret){
            if(err){
                console.log('保存失败')
            } else {
                console.log('保存成功')
                console.log(ret)
            }
        })
    }
    console.log(arr);
    console.log("end");
}

// 详情
async function detail(url){
    /* 1.新建一个浏览器的实例 */
    const browser = await puppeteer.launch({
        headless: true
    });
    /* 2.新建一个页面 */
    const page = await browser.newPage();
    /* 3.到达某个页面 */
    await page.goto("https://www.hiwuhuan.com"+url);
    /* 4.在页面停顿3s */
    /* 5.evaluate可以获取页面内容 */
    var content = await page.content();
    var $ = cheerio.load(content, {
        decodeEntities: false
    })
    var detail = [];
    let shoeName  = $(".fjbox ul li").eq(0).find(".sp_2").eq(0).html();
    let brand = $(".fjbox ul li").eq(0).find(".sp_2").eq(1).html();
    let shoeCode = $(".fjbox ul li").eq(1).find(".sp_2").eq(0).html();
    let shoeFor = $(".fjbox ul li").eq(1).find(".sp_2").eq(1).html();
    let sizeChart = $(".fjbox>div img").attr("src");
    let imgUl = $(".pro_img");
    let detailImg=[];
    imgUl.each((index,item)=>{
        detailImg.push($(item).attr("src"))
    })
    detail.push({
        shoeName,
        brand,
        shoeCode,
        shoeFor,
        sizeChart,
        detailImg
    })
    // console.log(detail);
    /* 6.关闭浏览器 */
    await browser.close();
    return detail
}
puppeteerData("Mizuno")